class code2 {
    //方法二：Map+stack
    public boolean isValid(String s) {
        Map<Character,Character> map = new HashMap<>() {{
            put('(',')');
            put('[',']');
            put('{','}');
        }};

        Stack<Character> stack = new Stack<>();

        for(char ch:s.toCharArray()) {
            if(map.containsKey(ch)) {
                //左括号
                stack.push(map.get(ch));
            } else if(stack.isEmpty()||stack.pop()!=ch) {
                //右括号(栈空|不匹配的右括号)
                return false;
            }
        }
        return stack.isEmpty();
    }
}